﻿using M.Warehouse.API.Application.Command.BinLocationsCommand;
using M.Warehouse.Domain;
using M.Warehouse.Domain.DTO;
using M.Warehouse.Repository.WarehouseInterface;
using MediatR;
using Microsoft.AspNetCore.Http.HttpResults;
using System.Collections.Generic;

namespace M.Warehouse.API.Application.CommandHandlers.BinLocationsCommandHandlers
{
    /// <summary>
    /// 显示库位信息
    /// </summary>
    public class BinLocationsListCommandHandler : IRequestHandler<BinLocationsListCommand, PageDTO<WarehBinDto>>
    {
        
        /// <summary>
        /// 库位仓储
        /// </summary>
        private readonly IBinLocationsRepository _repository;
        private readonly IReservoirsWRepository _reservoirsWRepository;
        private readonly IWarehouseWRepository _warehouseWRepository;
        public BinLocationsListCommandHandler(IBinLocationsRepository repository, IReservoirsWRepository reservoirsWRepository,
            IWarehouseWRepository warehouseWRepository)
        {
            _repository = repository;
            _reservoirsWRepository = reservoirsWRepository;
            _warehouseWRepository = warehouseWRepository;
        }
        public async Task<PageDTO<WarehBinDto>> Handle(BinLocationsListCommand request, CancellationToken cancellationToken)
        {
            var BinList = (from a in await _warehouseWRepository.GetAllAsync()
                           join b in await _reservoirsWRepository.GetAllAsync()
                           on a.WarehouseId equals b.WarehouseId
                           join c in await _repository.GetAllAsync()
                           on b.ReservoirId equals c.ReservoirId
                           where !c.IsDeleted
                           select new WarehBinDto
                           {
                               BinLocationId = c.BinLocationId,
                               BinLocationNo = c.BinLocationNo,
                               BinLocationName = c.BinLocationName,
                               BinLocationArea = c.BinLocationArea,
                               MaximumLoad = c.MaximumLoad,
                               BinLocationNotes = c.BinLocationNotes,
                               BinLocationState = c.BinLocationState,
                               WarehouseId = a.WarehouseId,
                               ReservoirId = b.ReservoirId,
                               WarehouseName = a.WarehouseName,
                               ReservoirName = b.ReservoirName,
                               CreatedBy=c.CreatedBy,
                               CreatedDate=c.CreatedDate,
                               UpdatedBy=c.UpdatedBy,
                               UpdatedDate=c.UpdatedDate,
                               DeletedBy=c.DeletedBy,
                               DeletedDate=c.DeletedDate,
                               IsDeleted=c.IsDeleted,

                           }).ToList();
            if (!string.IsNullOrEmpty(request.BinLocationNo))
            {
                BinList = BinList.Where(d => d.BinLocationNo.Contains(request.BinLocationNo)).ToList();
            }
            if (!string.IsNullOrEmpty(request.BinLocationName)) 
            { 
                BinList=BinList.Where(d=>d.BinLocationName.Contains(request.BinLocationName)).ToList();
            }
            if (!string.IsNullOrEmpty(request.BinLocationState))
            {
                bool start = false;
                if (request.BinLocationState == "禁用")
                {
                    start=true;
                }
                BinList=BinList.Where(d=>d.BinLocationState==start).ToList();
            }
            if (request.PageSize == null)
            {
                request.PageSize = BinList.Count();
            }
            if (request.PageIndex == null)
            {
                request.PageIndex = 1;
            }
            PageDTO<WarehBinDto> page = new PageDTO<WarehBinDto>();
            page.TotalCount = BinList.Count();
            page.PageCount = (int)Math.Ceiling(page.TotalCount * 1.0 / (int)request.PageSize);
            page.list = BinList.OrderByDescending(x => x.CreatedDate).Skip((int)(request.PageIndex - 1) * (int)request.PageSize).Take((int)request.PageSize).ToList();
            return page;
        }
    }
}
